<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="是否显示" prop="bak1">
        <el-select v-model="queryParams.bak1" placeholder="请选择是否显示" clearable size="small">
          <el-option
            v-for="dict in bak1Options"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['member:banner:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['member:banner:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['member:banner:remove']"
        >删除</el-button>
      </el-col>
    <!--  <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['member:banner:export']"
        >导出</el-button>
      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="bannerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
     <!-- <el-table-column label="备用字段3" align="center" prop="bak3" />
      <el-table-column label="备用字段2" align="center" prop="bak2" />-->


      <el-table-column label="图片路径" align="center" prop="imgPath" >
        <template slot-scope="scope">
          <el-image :src="imgpre+ scope.row.imgPath"  :preview-src-list="showImg(scope.row.imgPath)"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="是否显示" align="center" prop="bak1" :formatter="bak1Format" />
    <!--  <el-table-column label="简介" align="center" prop="introduction" />-->
  <!--    <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['member:banner:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['member:banner:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改banner图对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--<el-form-item label="备用字段3" prop="bak3">
          <el-input v-model="form.bak3" placeholder="请输入备用字段3" />
        </el-form-item>
        <el-form-item label="备用字段2" prop="bak2">
          <el-input v-model="form.bak2" placeholder="请输入备用字段2" />
        </el-form-item>-->
        <el-form-item label="图片路径" prop="imgPath">
          <el-upload
            class="avatar-uploader"
            :action="uploadImgUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.imgPath" :src="imgpre +form.imgPath" class="avatarbanner">
            <i v-else class="el-icon-plus avatar-uploader-iconbanner"></i>
            <div slot="tip" class="el-upload-tip">.支持jpg、png、gif、jpeg格式的图片</div>
            <div slot="tip" class="el-upload-tip">.图片尺寸1420PX*600PX，文件大小不超过2M</div>
          </el-upload>

        </el-form-item>
        <el-form-item label="是否显示">
          <el-select v-model="form.bak1" placeholder="请选择是否有效">
            <el-option
              v-for="dict in bak1Options"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="简介" prop="introduction">
          <el-select v-model="form.introduction" filterable placeholder="请选择文章">
            <el-option
              v-for="item in articleList"
              :key="item.id"
              :label="item.title"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBanner, getBanner, delBanner, addBanner, updateBanner, exportBanner } from "@/api/member/banner";
import { listAll } from "@/api/member/article";
import { handerImg } from "@/api/member/utils";
import defaultSettings from '@/settings'
export default {
  name: "Banner",
  data() {
    return {
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload/oss" ,
      imgpre: defaultSettings.ossPreview,// 上传的图片服务器地址
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      options:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // banner图表格数据
      bannerList: [],
      articleList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否有效字典
      bak1Options: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bak1: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        imgPath: [
          { required: true, message: "图片路径不能为空", trigger: "blur" },
          { required: true, bak1: "是否有效不允许为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_yes_no").then(response => {
      this.bak1Options = response.data;
    });
    this.getArticle();
  },
  methods: {
    handleAvatarSuccess(res) {
      this.form.imgPath =  res.imgUrl
    },

    beforeAvatarUpload(file) {


      const isJPG = handerImg(file.type);

      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },

    //放大图片
    showImg(e){
      let array=[];
      array.push(this.imgpre+e);
      return array;
    },
    /** 查询banner图列表 */
    getList() {
      this.loading = true;
      listBanner(this.queryParams).then(response => {
        this.bannerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

    },
    getArticle(){
      listAll({}).then(response => {
        this.articleList = response.data;
      });
    },
    // 是否有效字典翻译
    bak1Format(row, column) {
      return this.selectDictLabel(this.bak1Options, row.bak1);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        updateTime: undefined,
        updateBy: undefined,
        createTime: undefined,
        createBy: undefined,
        bak3: undefined,
        bak2: undefined,
        bak1: undefined,
        introduction: undefined,
        imgPath: undefined,
        id: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加banner图";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBanner(id).then(response => {
        this.form = response.data;
        this.form.introduction= parseInt(this.form.introduction);
        this.open = true;
        this.title = "修改banner图";
      });
    },
    /** 提交按钮 */
    submitForm: function() {

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateBanner(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addBanner(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除banner图编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBanner(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有banner图数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportBanner(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-iconbanner {
    font-size: 28px;
    color: #8c939d;
    width: 355px;
    height: 150px;
    line-height: 150px;
    text-align: center;
  }
  .avatarbanner {
    width: 355px;
    height: 150px;
    display: block;
  }
  .el-upload-tip{
    color: #AAAAAA;
    line-height: 1.7;
    font-size: 12px;
  }
</style>
